import random
import time
#身份验证模块
def SF (DB:list[dict]):
    while True:
        id = input("请输入您的账户(退出请输入0):")

        if id == "0":
            print("已退出身份验证,登录失败！")
            return None
        
        test = 0
        for i in DB:
            if id == i["account"]:
                test = 1
                password = input("请输入密码：")
                if password == i["password"]:
                    print(f"身份验证成功，欢迎 {i['name']}!")
                    return i
                else:
                    print("密码错误,请重新输入账户或退出(输入0退出)。")
                    break  # 密码错跳出当前for循环，重新输入账户
        if test == 0:
            print("账户不存在，请重新输入:退出请输入0")
          

#开户
def KH (DB:list[dict]):
    #声明一下DB是一个列表嵌套字典
    #生成随机账户并确保不重复
    while True:
        NEWaccount = ''.join(str(random.randint(1,9)) for _ in range(1,9))
        test = 0
        for i in DB:
            if i["account"] == NEWaccount:
                test = 1
                break
        if test == 1:
            continue
        else:
            break
    #接收输入的数据：
    New = {}
    New["name"] = input("请输入姓名")
    New["account"] = NEWaccount
    New["password"] = input("请输入密码：")
    New["balance"] = int(input("请输入要存的金额："))
    New["bank"] = input("请输入开户行名：") 
    New["type"] = (1 if New["balance"] > 100000 else 2)
    New["address"] = input("请输入您的住址：")             
    #写入数据库
    DB.append(New)

    print("********************************")
    print("*         开户  成功！         *")      
    print("********************************")     
    print(f"您的账户信息为：{New}")                
#存钱
def CQ (DB:list[dict]):
    user = SF(DB)
    if user != None:
        print(f"您的账户余额为：{user['balance']}")
        Money = int(input("请输入要存入的金额："))
        user["balance"] += Money

        print("********************************")
        print(f"* 存钱成功！，您的余额为{user['balance']} *")
        print("********************************")
        time.sleep(2)
    else:
        print("身份验证失败！无法存款") 
        time.sleep(2)      
#取钱
def QQ (DB:list[dict]):
    User = SF(DB)
    if User != None:
        while True:
            print(f"您的账户余额为：{User['balance']}")
            Money = int(input("请输入取款金额："))
            if Money > User["balance"]:
                print("您的余额不足，请重新输入！")
                continue
            else:
                User["balance"] -= Money
                print(f"取款成功，您的账户余额为：{User['balance']}")
                time.sleep(2)
                break
    else:
        print("身份验证失败，取钱结束！")
        time.sleep(2)
#转账
def ZZ (DB:list[dict]):
    User = SF(DB)
    if User != None:
        while True:
            print(f"您的余额为：{User["balance"]}")
            Zmoney = int(input("请输入要转账的数额：")) 
            if Zmoney > User["balance"]:
                print("金额不足，请重新输入！")
                continue
            else:
                 while True:
                    ZZ_name = input("请输入转入账户号(退出请输入0):")
                    if ZZ_name == "0":
                        print("已退出,转账失败！")
                        time.sleep(2)
                        return None
                    test = 0
                    for i in DB:
                        if ZZ_name == i["account"]:
                            test = 1
                            User["balance"] -= Zmoney
                            i["balance"] += Zmoney
                            print(f"转账成功，您的余额为：{User["balance"]}")
                            time.sleep(2)
                            return
                    if test == 0:
                        print("账户不存在")
                                                                                                                                             



    else:
        print("身份验证失败，转账结束！")
        time.sleep(2)
#查询
def CX (DB:list[dict]):
    User = SF(DB)
    if User != None:
       print(f"姓名：{User["name"]}")
       print(f"账户：{User["account"]}")
       print(f"余额：{User["balance"]}")
       print(f"开户行：{User["bank"]}")
       print(f"等级：{User["type"]}")
       print(f"地址：{User["address"]}")
       time.sleep(5)
    else:
        print("身份验证失败，查询结束！")
        time.sleep(2)




####################################################################################################################################################################
DataBase = [
        {"name": "张三", "account": "12345678", "password": "123456", "balance": 5000, "bank": "中国农业银行的昌平沙河支行","type":1,"address":"鲁山县张良镇"},
        {"name": "李四", "account": "87654321", "password": "654321", "balance": 3000, "bank": "中国农业银行的平顶山市支行","type":2,"address":"河南省郑州市"},
        {"name": "王五", "account": "11223344", "password": "111111", "balance": 10000, "bank": "中国农业银行的昌平沙河支行","type":1,"address":"河北省黄河市"},
        {"name": "赵六", "account": "44332211", "password": "222222", "balance": 7000, "bank": "中国农业银行的郑州市支行","type":2,"address":"上海交通路"},
        {"name": "孙七", "account": "99887766", "password": "333333", "balance": 2500, "bank": "中国农业银行的鲁山县支行","type":1,"address":"北京天安门"}
    ]
while True:
    print("********************************")
    print("*   中国农业银行账户管理系统   *")
    print("********************************")
    print("*      请输入序号选择服务：    *")
    print("*            1.开户            *")
    print("*            2.存钱            *")
    print("*            3.取钱            *")
    print("*            4.转账            *")
    print("*            5.查询            *")
    print("*            6.退出            *")
    print("********************************")
    Select = int(input("请选择："))
    if Select == 1:
        KH(DataBase)
    elif Select == 2:
        CQ(DataBase)
    elif Select == 3:
        QQ(DataBase)
    elif Select == 4:
        ZZ(DataBase)
    elif Select == 5:
        CX(DataBase)
    elif Select == 6:
        print("********************************")
        print("*         系统已退出！         *")
        print("********************************")
        break
    else:
        print("请重新输入有效选择：")
        continue
